﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for CAgent
/// </summary>
public class CAgent
{
	public CAgent()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public bool Add(string title,string name,string pwd)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        object obj = d.ExeScalar("select count(*) from agent", null);
        pars.Add("Title", title);
        pars.Add("SortCode", Convert.ToInt32(obj) + 1);
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        int c = d.ExeNonQuery("insert into agent(title,sortcode,username,userpwd) values(@Title,@SortCode,@Name,@Pwd)", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool CheckName(string name)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        object obj = d.ExeScalar("select count(*) from agent where username=@Name", pars);
        d.Close();
        if (obj.ToString() != "0")
        {
            return true;
        }
        return false;
    }

    public bool Update(int id,string title,string name,string pwd)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Title", title);
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update agent set title=@Title,username=@Name,userpwd=@Pwd where id=@ID", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Delete(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from agent where ID=@ID", pars);
        int c = d.ExeNonQuery("delete from agent where id=@ID", pars);
        pars.Clear();
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["SortCode"].ToString()));
        c += d.ExeNonQuery("update agent set sortcode=sortcode-1 where  sortcode > @SortCode", pars);
        d.Close();
        if (c > 0)
        {
            return true;
        }
        return false;
    }

    public bool Sort(int id, bool isUp)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from agent where id=@ID", pars);
        pars.Clear();
        pars.Add("SortCode", Convert.ToInt32(dt.Rows[0]["sortcode"].ToString()));
        if (isUp)
        {
            dt = d.ExeDataTable("select top 1 * from agent where sortcode<@SortCode order by sortcode desc", pars);
        }
        else
        {
            dt = d.ExeDataTable("select top 1 * from agent where sortcode >@SortCode order by sortcode asc", pars);
        }
        pars.Clear();
        pars.Add("ID", id);
        int c = d.ExeNonQuery("update agent set sortcode=" + (isUp ? "sortcode-1" : "sortcode+1") + " where id=@ID", pars);
        pars.Clear();
        if (null != dt)
        {
            pars.Add("ID", Convert.ToInt32(dt.Rows[0]["id"].ToString()));
            c += d.ExeNonQuery("update agent set sortcode=" + (isUp ? "sortcode+1" : "sortcode-1") + " where id=@ID", pars);
        }
        d.Close();
        if (c > 0)
        {
            return true;
        }
        d.Close();
        return false;
    }

    public DataTable ReadAgents()
    {
        DataAccess d = new DataAccess();
        d.Open();
        DataTable dt = d.ExeDataTable("select * from agent order by sortcode asc", null);
        d.Close();
        return dt;
    }

    public DataRow ReadAgent(int id)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("ID", id);
        DataTable dt = d.ExeDataTable("select * from agent where id=@ID", pars);
        d.Close();
        if (null != dt)
        {
            return dt.Rows[0];
        }
        return null;
    }

    public bool Login(string name,string pwd)
    {
        DataAccess d = new DataAccess();
        d.Open();
        Dictionary<object, object> pars = new Dictionary<object, object>();
        pars.Add("Name", name);
        pars.Add("Pwd", pwd);
        object obj = d.ExeScalar("select count(*) from agent where username=@Name and userpwd=@Pwd", pars);
        d.Close();
        if (obj.ToString() != "0")
        {
            return true;
        }
        return false;
    }
}